﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Skimpt3
{
    /// <summary>
    /// Interaction logic for Camera.xaml
    /// </summary>
    public partial class Camera : Window
    {
        //Vars for dragging
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd,
                         int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        public Camera()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            MouseDown += delegate { DragMove(); };
        }

        private void OnKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if(e.Key == Key.Escape)
            {
                this.Close();
            }

            if (e.Key == Key.Enter)
            {
                this.Hide();
                Bitmap bitmap;
                bitmap = ScreenCapture.GetDesktopWindowCaptureAsBitmap();

                System.Drawing.Rectangle rect = new System.Drawing.Rectangle((int)this.Left, 
                                                                             (int)this.Top, 
                                                                             (int)this.Width, 
                                                                             (int)this.Height);

                System.Drawing.Image croppedImage = (System.Drawing.Image)bitmap.Clone(rect, bitmap.PixelFormat);

                PreviewImage imageWindow = new PreviewImage();
                imageWindow.Height = croppedImage.Height;
                imageWindow.Width = croppedImage.Width;

                PictureBox picBox = new PictureBox();
                picBox.Height = croppedImage.Height;
                picBox.Width = croppedImage.Width;
                picBox.Image = croppedImage;

                imageWindow.Controls.Add(picBox);
                DialogResult res = imageWindow.Verify();
                this.Hide();

                if (res == System.Windows.Forms.DialogResult.No)
                {
                    this.Show();
                    imageWindow.Close();
                }

                if (res == System.Windows.Forms.DialogResult.Yes)
                {
                    Upload uploadWindow = new Upload();
                    uploadWindow.img = croppedImage;
                    uploadWindow.Show();
                    this.Close();
                }
            }
        }
    }
}
